package cn.agiledata.bank.common.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.agiledata.bank.common.dao.AccountTemplateInter;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.ForeDict;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.common.util.WebUtil;
import cn.agiledata.bank.hibernateBean.Account;
import com.vnv.infrastructure.action.IPBBaseAction;
import com.vnv.infrastructure.logic.IPBAppFactory;

public class RouteAction extends IPBBaseAction {

	private AccountTemplateInter getAccountTemplate() {
        return (AccountTemplateInter) IPBAppFactory
                .getObj(ForeDict.ACCOUNT_TEMPLATE);
    }
	/**
	 * 取得当前账户
	 * 
	 * @param request
	 * @return
	 */
    private Account getCurrentAccount(HttpServletRequest request) {
        long accountId = WeatherUtil.getAccountIdFromSession(request);
        return getAccountTemplate().getAccount(String.valueOf(accountId));
    }
	/**
	 * 
	 */
	public ActionForward route(ActionMapping mapping, ActionForm actionForm,
            HttpServletRequest request, HttpServletResponse response) {
		String modual = request.getParameter("modual");
		if(modual != null) {
			String[] strings = StringUtils.split(modual, "$");
			if(strings != null) {
				Account account = getCurrentAccount(request);
				WebUtil.setSessionString(request,"modual",strings[0]);
				//解决在注册页面点相关功能链接 然后再以网关商户登录后 跳转混乱的问题 20080101
				if (Constant.COMPANY_TYPE_GATEWAY.equals(account.getCompanyType())){
					if (!strings[0].equals("custInfo")
							& !strings[0].equals("return")
							& !strings[0].equals("manage")
							& !strings[0].equals("space")){
						WebUtil.setSessionString(request,"modual","space");
					} else if (strings[0].equals("manage")
							& !modual.equals("manage$history")
							& !modual.equals("manage$query")
							& !modual.equals("manage$print")) {
						return mapping.findForward("manage$gateWayTransQuery");
					}
				}
				//20080401end
			}
			return mapping.findForward(modual);
		}
		return mapping.findForward("main");
	}
}
